//
//  XUTabBar.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-9.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//

#import "XUTabBar.h"
#import "UIView+Frame.h"

@interface XUTabBar ()

@property (nonatomic, weak) UIButton *btn;

@end

@implementation XUTabBar

// 排列TabBar中的按钮
- (void)layoutSubviews
{
    [super layoutSubviews];
//    NSLog(@"------%@",self.subviews);
    CGFloat viewW = [UIScreen mainScreen].bounds.size.width;
    NSInteger index = 0;
    for (int i = 0; i<self.subviews.count;i++) {
//        NSLog(@"%@",[self.subviews[i] superclass]);
        // 因为UITabBarButton是系统控件,,不允许用,, 只能判断父类
        if ([self.subviews[i] isKindOfClass:[UIControl class]]) {
            UIView *view = self.subviews[i];
            
            if (index == 2) {
                index ++;
            }
            CGFloat margin = viewW / 5 * 0.5;
            // 计算中心点,,更精确
//            view.x = viewW / 5 * index;
            view.centerX = margin + index * margin * 2;
            index++;
        }
        
        self.btn.centerX = self.width * 0.5;
        self.btn.centerY = self.height * 0.5;
    }
}

- (UIButton *)btn
{
    if (_btn == nil) {
        
        UIButton *btn = [[UIButton alloc] init];
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [btn sizeToFit];
        [self addSubview:btn];
        _btn = btn;
    }
    return _btn;
}
- (void)click:(UIButton *)btn
{
//    if ([_XUdelegate respondsToSelector:@selector(XUTabBarDelegateWithdelegate)]) {
//        [_XUdelegate XUTabBarDelegateWithdelegate];
//    }
    if (self.myBlock != nil) {
        self.myBlock();
    }
    
    
    NSLog(@"%s",__func__);
}

//- (void)drawRect:(CGRect)rect {
//    
//    
//    
//}


@end
